package com.htg.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.core.widget.ContentLoadingProgressBar;



public class DialogAMapDownloading {

    private Context cxt;

    private AlertDialog alertDialog = null;

    private ContentLoadingProgressBar contentLoadingProgressBar;


    public DialogAMapDownloading(Context cxt) {
        this.cxt = cxt;
    }

    public void show(OnClickListener onClickListener) {
        if (alertDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(cxt);
            alertDialog = builder.create();
            alertDialog.setCancelable(false);
        } else {
            if (alertDialog.isShowing()) {
                return;
            }
        }
        alertDialog.show();
        View view = LayoutInflater.from(cxt).inflate(R.layout.dialog_amap_downloading, null);
        Window window = alertDialog.getWindow();
        window.setBackgroundDrawableResource(R.drawable.shape_white_rounded_12dp);
        WindowManager.LayoutParams params = window.getAttributes();
        int width = cxt.getResources().getDimensionPixelSize(R.dimen.dp_280);

        params.width = width;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        //window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,WindowManager.LayoutParams.WRAP_CONTENT);
        window.setContentView(view);

        contentLoadingProgressBar = view.findViewById(R.id.cpb_dialog_amap_downloading);
        TextView tvCancel = view.findViewById(R.id.tv_dialog_amap_downloading_cancel);

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onClickListener!=null){
                    onClickListener.onClick(alertDialog);
                }
            }
        });


    }


    public void updateProgress(int progress) {
        contentLoadingProgressBar.setProgress(progress);
    }


    public void dismis() {
        if (alertDialog != null && alertDialog.isShowing()) {
            alertDialog.dismiss();
        }
        alertDialog = null;
    }

    public interface OnClickListener{
        void onClick(AlertDialog alertDialog);
    }


}
